{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "7b833fbc",
   "metadata": {},
   "source": [
    "# 数据类型转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "a15c9863",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'int'>\n",
      "我叫张三今年,20岁\n"
     ]
    }
   ],
   "source": [
    "name='张三'\n",
    "age=20\n",
    "print(type(name),type(age))\n",
    "\n",
    "#说明name和age的数据类型不相同\n",
    "\n",
    "print('我叫'+name+'今年,'+str(age)+'岁')\n",
    "#str是将其它的类型转换成字符串\n",
    "#此时这个+号是连接符，将int类型通过str()函数转成了str类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "936dd4f4",
   "metadata": {},
   "source": [
    "## str()将其它类型专程str类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d5571fce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'int'> <class 'float'> <class 'bool'>\n"
     ]
    }
   ],
   "source": [
    "a=10\n",
    "b=198.8\n",
    "c=False\n",
    "print(type(a),type(b),type(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "27f08ee8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 198.8 False\n"
     ]
    }
   ],
   "source": [
    "print(str(a),str(b),str(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "392325f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 198.8 False <class 'str'> <class 'str'> <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "print(str(a),str(b),str(c),type(str(a)),type(str(b)),type(str(c)))\n",
    "#此时我们会发现都变成了 str类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6aefcfc",
   "metadata": {},
   "source": [
    "## int()将其它的类型转int类型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "145072b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'float'> <class 'str'> <class 'bool'> <class 'str'>\n",
      "128 <class 'int'>\n",
      "98 <class 'int'>\n",
      "1 <class 'int'>\n"
     ]
    },
    {
     "ename": "ValueError",
     "evalue": "invalid literal for int() with base 10: '76.77'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/0y/9ngk9zq509d_968nb4blyvrm0000gn/T/ipykernel_2056/3548845064.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mf1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mff\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mff\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: '76.77'"
     ]
    }
   ],
   "source": [
    "s1='128'\n",
    "f1=98.7\n",
    "s3='76.77'\n",
    "ff=True #float类型\n",
    "s2='hello'#也是不能转的，将str类型转成int类型，字符串必须为数字串，整数\n",
    "#非数字串是不允许转换的\n",
    "print(type(s1),type(f1),type(s2),type(ff),type(s2))\n",
    "print(int(s1),type(int(s1)))\n",
    "print(int(f1),type(int(f1)))\n",
    "print(int(ff),type(int(ff)))\n",
    "#print(int(s3),type(int(s3)))\n",
    "#将str转成int类型，报错，因为字符串为小数串，所以会报错\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3e50e15a",
   "metadata": {},
   "source": [
    "## float函数转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b91f4326",
   "metadata": {},
   "outputs": [],
   "source": [
    "#将其它数据去转换成float"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "361fc4da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'str'> <class 'float'> <class 'str'> <class 'bool'> <class 'str'> <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "s1='128'\n",
    "f1=98.7\n",
    "s3='76.77'\n",
    "ff=True #float类型\n",
    "s2='hello'\n",
    "i=98\n",
    "print(type(s1),type(f1),type(s3),type(ff),type(s2),type(i))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2598e4e1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.0 <class 'float'>\n"
     ]
    }
   ],
   "source": [
    "print(float(ff),type(float(ff)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "a24061e8",
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "could not convert string to float: 'hello'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m/var/folders/0y/9ngk9zq509d_968nb4blyvrm0000gn/T/ipykernel_2056/3904981877.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m: could not convert string to float: 'hello'"
     ]
    }
   ],
   "source": [
    "print(float(s2),type(float(s2)))\n",
    "#字符串中的数据如果是非数字串，则不允许转换"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "99c0c974",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "264.796875px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
